﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using DG.Tweening;

public class UIPageHandle : UIBaseTab
{


    /// <summary>
    /// 初始化填充数量，并且绑定事件
    /// </summary>
    /// <param name="page"></param>
    /// <param name="pageMax"></param>
    public void Init(int page = 0, int pageMax = 5)
    {
        int gap = pageMax - this.transform.childCount;
        foreach (Transform trans in this.transform)
        {
           trans.gameObject.SetActive(true);
        }
        
        var source = this.transform.GetChild(0);
        if (gap > 0)
        {
            //补充缺少的
            for (int i = 0; i < gap; i++)
            {
                var child = Instantiate(source);
                child.SetParent(this.transform);
                child.transform.localScale = Vector3.one;
                child.transform.localEulerAngles = Vector3.one;
            }
        }
        else
        {
            //销毁多余的
            foreach (Transform trans in this.transform)
            {
                if (trans.GetSiblingIndex() >= pageMax)
                {
                    trans.gameObject.SetActive(false);
                }
            }
        }

        //重新更新按钮绑定事件
        base.Init();

    }

    

    public override void OnButtonDown(int index)
    {
        var selectObject = this.transform.GetChild(index);
        selectObject.transform.DOPunchScale(Vector3.one * 0.1f, 0.5f);
    }



    protected override void UpdateTabState(GameObject handle, bool selected)
    {
        Graphic graphic = null;
        if (handle.transform.GetChild(0))
        {
            graphic = handle.transform.GetChild(0).GetComponent<Graphic>();
        }
        else
        {
            graphic = handle.GetComponent<Graphic>();
        }

        if (graphic)
        {
            graphic.color = selected ? Color.white : new Color(1, 1, 1, 0.6f);
        }

    }


}
